package com.xinqi.printByThreadsWithLoop.bysynchronizedForever.august2;

/**
 * @author xin麒
 * @date 2023/8/2 15:45
 * 2个线程打印分别按顺序打印oddNumber or evenNumber
 */

public class XinThread {
    public static void main(String[] args) {
        MyTask myTask = new MyTask();
        new Thread(()->{
            myTask.print(1);
        }).start();

        new Thread(()->{
            myTask.print(0);
        }).start();


    }
}
class MyTask {
    int count = 1;
    public void print(int n){
        while (true){
            synchronized (this){
                try {
                    if (count % 2 != n)  this.wait();
                    System.out.println(count++);
                    Thread.sleep(1 * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                this.notifyAll();
            }
        }
    }


}
